package com.githan56.mybatis_test.controller;

import com.githan56.mybatis_test.entity.Book;
import com.githan56.mybatis_test.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author https://github.com/Han56
 * @date 2021/3/26 19:57
 * 描述：控制层
 */
@RestController
public class BookController {
    @Autowired
    BookService bookService;
    @GetMapping("/bookOps")
    public void BookOps(){
        Book b1 = new Book();
        b1.setName("西厢记");
        b1.setAuthor("王世谱");
        int i = bookService.addBook(b1);
        System.out.println("addBook>>>"+i);

        Book b2 = new Book();
        b2.setId(1);
        b2.setName("朝花夕拾");
        b2.setAuthor("鲁迅");
        int j = bookService.updateBookById(b2);
        System.out.println("updateBook>>>"+j);

        int deleteBook = bookService.deleteBookById(2);
        System.out.println("delete>>>"+deleteBook);

        List<Book> allBooks = bookService.getAllBooks();
        for (Book book:allBooks){
            System.out.println("[id]"+book.getId()+"[name]"+book.getName()+"[author]"+book.getAuthor());
        }
        System.out.println("getAllBooks"+allBooks);

    }
}
